package com.smugmugsoftware.airconcalc.advanced;

import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class ConstNoWizardDialog extends DialogFragment implements android.content.DialogInterface.OnClickListener {

	MainActivityInterface activity;

	private Context context;
	private String[] type;
	private Vector<String> indexList;
	private TableParser parser;
	private DialogFragment thisDialog;

	public ConstNoWizardDialog() {}

	public ConstNoWizardDialog(Context context, String[] type) {
		this.type = type;
		this.context = context;
		this.indexList = new Vector<String>();
		this.thisDialog = this;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			this.activity = (MainActivityInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		ScrollView scroller = new ScrollView(context);
		scroller.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.WRAP_CONTENT, ScrollView.LayoutParams.MATCH_PARENT));

		String[] list = null;

		parser = new TableParser(context, R.raw.heatgain);

		for (int i = 0; i < type.length; i++) {
			indexList.add(type[i]);
		}
		
		list = parser.getSubIndex((String[]) type);

		if (type[0] == "Walls") {
			if (indexList.size() == 1) {
				builder.setTitle("Select Wall Type");
			} else if (indexList.size() == 3) {
				builder.setTitle("Select Wall Material");
			} else if (indexList.size() == 4) {
				builder.setTitle("Select Insulation Type");
			}

		} else if (type[0] == "Ceilings & Floors") {

			if (indexList.size() == 1) {
				builder.setTitle("Select Construction Orientation");
			} else if (indexList.size() == 3) {
				builder.setTitle("Select Type");
			} else if (indexList.size() == 4) {
				builder.setTitle("Select Insulation");
			}

		} else if (type[0] == "Windows & Doors") {

			if (indexList.size() == 1) {
				builder.setTitle("Select Type");
			} else if (indexList.size() == 3 && type[1].startsWith("Windows and Glass")) {
				builder.setTitle("External Solar Shades");
			} else if (indexList.size() == 4 && type[1].startsWith("Windows and Glass")) {
				builder.setTitle("Select Glass Treatment");
			} else if (indexList.size() == 5 && type[1].startsWith("Windows and Glass")) {
				builder.setTitle("Select Internal Shading");
			} else if (indexList.size() == 6 && type[1].startsWith("Windows and Glass")) {
				builder.setTitle("Select Glass Thickness");
			}
		}

		builder.setItems(list, this);
		return builder.create();
	}

	public void onClick(DialogInterface dialog, int which) {
			if (indexList.size() == 1) {
				
				if ((((AlertDialog) dialog).getListView().getAdapter().getItem(which)).toString().startsWith("All Walls ")) {
					thisDialog.dismiss();
					activity.getStringFromDialog(NewRoomFragmentInterface.WALL_CONST_NO, "15-A");
					return;
				} else if ((((AlertDialog) dialog).getListView().getAdapter().getItem(which)).toString().startsWith("All Ceilings ")) {
					thisDialog.dismiss();
					activity.getStringFromDialog(NewRoomFragmentInterface.CEILING_FLOOR_CONST_NO, "24-A");
					return;
				} else if ((((AlertDialog) dialog).getListView().getAdapter().getItem(which)).toString().startsWith("All Floors ")) {
					thisDialog.dismiss();
					activity.getStringFromDialog(NewRoomFragmentInterface.CEILING_FLOOR_CONST_NO, "19-A");
					return;
				}
				
				indexList.add((((AlertDialog) dialog).getListView().getAdapter().getItem(which)).toString());
				indexList.add("List");

				thisDialog.dismiss();
				activity.displayConstNoDialog(context, (String[]) indexList.toArray(new String[indexList.size()]));

			} else if (indexList.size() == 3) {
				
				indexList.add((((AlertDialog) dialog).getListView().getAdapter().getItem(which)).toString());
				thisDialog.dismiss();

				if (indexList.get(1).endsWith(" Doors") && !(type[2].endsWith(" Glass Doors"))) {
					activity.getStringFromDialog(NewRoomFragmentInterface.WINDOW_DOOR_CONST_NO, 
							parser.getItemNumber((String[]) indexList.toArray(new String[indexList.size()])));
					return;
				}

				activity.displayConstNoDialog(context, (String[]) indexList.toArray(new String[indexList.size()]));

			} else if ((type[0] == "Walls" || type[0] == "Ceilings & Floors") && indexList.size() == 4) {
				thisDialog.dismiss();

				indexList.add((((AlertDialog) dialog).getListView().getAdapter().getItem(which)).toString());
				
				if (type[0] == "Walls") {
					activity.getStringFromDialog(NewRoomFragmentInterface.WALL_CONST_NO, parser.getItemNumber((String[]) indexList.toArray(new String[indexList.size()])));
				} else if (type[0] == "Ceilings & Floors") {
					activity.getStringFromDialog(NewRoomFragmentInterface.CEILING_FLOOR_CONST_NO, parser.getItemNumber((String[]) indexList.toArray(new String[indexList.size()])));
				}
				
			} else if (type[0] == "Windows & Doors") {
				
				
				if ((type[1].startsWith("Windows and Glass") && indexList.size() == 6)
						|| (type[1].startsWith("Skylights") && indexList.size() == 5)) {
					indexList.add((((AlertDialog) dialog).getListView().getAdapter().getItem(which)).toString());
					thisDialog.dismiss();
					activity.getStringFromDialog(NewRoomFragmentInterface.WINDOW_DOOR_CONST_NO, 
							parser.getItemNumber((String[]) indexList.toArray(new String[indexList.size()])));

				} else {
					indexList.add((((AlertDialog) dialog).getListView().getAdapter().getItem(which)).toString());
					thisDialog.dismiss();
					activity.displayConstNoDialog(context, (String[]) indexList.toArray(new String[indexList.size()]));
				}
			}
		}

	@Override
	public void onDestroyView() {
		if (getDialog() != null && getRetainInstance())
			getDialog().setOnDismissListener(null);
		super.onDestroyView();
	}
}
